package com.wangchao.administrator.zenmechi.ui.fragment.shopfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.ui.searchactivity.SearchActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/17.
 */
public class ShopFragment extends Fragment implements ShopFragmentContract.View, View.OnClickListener {

    @BindView(R.id.image_search)
    ImageView imageSearch;
    @BindView(R.id.shop_head)
    RelativeLayout shopHead;
    @BindView(R.id.lv_shop)
    ExpandableListView lvShop;
    @BindView(R.id.tv_shop_category)
    TextView tvShopCategory;
    @BindView(R.id.tv_hot)
    TextView tvHot;
    @BindView(R.id.show_layout)
    LinearLayout showLayout;
    @BindView(R.id.lv_shop_category)
    PullToRefreshListView lvShopCategory;
    @BindView(R.id.image_hide)
    ImageView imageHide;
    @BindView(R.id.layout_hide)
    RelativeLayout layoutHide;
    private ListView lv;
    private View view;
    private ShopFragmentPresenter presenter;
    private int cateNum = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shop, container, false);
        ButterKnife.bind(this, view);

        lvShopCategory.setMode(PullToRefreshBase.Mode.BOTH);

        presenter = new ShopFragmentPresenter(this);
        presenter.getCategoryData(getActivity());

        initClickEvent();
        return view;
    }

    private void initClickEvent() {
        imageSearch.setOnClickListener(this);


        lvShop.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView,
                                        View view, int i, long l) {
                String cateId = presenter.getCateId(i);
                String cateName = presenter.getCateName(i);

                for (int k=0;k<cateNum;k++){
                /*for (int k=0;k<cateNum;k++){
                    if (k!=i){
                        lvShop.collapseGroup(k);
                    }
                }
                }*/

                tvShopCategory.setText(cateName);
                presenter.getCateList(getActivity(), cateId);
                return false;
            }
        });

        lvShop.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView,
                                        View view, int i, int i1, long l) {

                for (int k=0;k<cateNum;k++){
                /*for (int k=0;k<cateNum;k++){
                    if (k!=i){
                        lvShop.collapseGroup(k);
                    }
                }
                }*/

                String childId = presenter.getCateChildsId(i, i1);
                String childName = presenter.getCateChildsName(i, i1);

                tvShopCategory.setText(childName);
                presenter.getCateList(getActivity(), childId);
                return false;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.image_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }


    @Override
    public void showLayout() {
        layoutHide.setVisibility(View.VISIBLE);
        lvShopCategory.setVisibility(View.GONE);
    }

    @Override
    public void hideLayout() {
        layoutHide.setVisibility(View.GONE);
        lvShopCategory.setVisibility(View.VISIBLE);
    }

    @Override
    public void initExpandListView(MyAdapterForShopExpand adapter) {
        lvShop.setAdapter(adapter);
        lvShop.requestFocus();
        lvShop.setFocusable(true);
        lvShop.setFocusableInTouchMode(true);

        cateNum = adapter.getGroupCount();
        for (int i = 0; i < adapter.getGroupCount(); i++) {
            lvShop.expandGroup(0);
        }
    }


    @Override
    public void showList(MyAdapterForShowList adapter) {
        lv = lvShopCategory.getRefreshableView();
        lv.setAdapter(adapter);
    }
}
